package controller

import (
	"github.com/gin-gonic/gin"
	"shop-bean/constant"
	"shop-bean/model"
	"shop-bean/request"
	"shop-common/library/serializer"
	"shop-common/utils/contextArgs"
	"shop-common/utils/pagehelper"
	"shop-common/utils/queryWrapper"
	"shop-service/business"
)

type NoticeController struct {
}

// @Tags 公告管理
// @description: 公告分页
// @Summary 公告分页
// @Security ApiKeyAuth
// @function: Page
// @Accept  json
// @Produce  json
// @Param current query string false "当前页，默认1"
// @Param size query string false "每页大小，默认10"
// @Param keyword query string false "keyword"
// @Param status query int false "状态  1:无效  2:正常"
// @Param isTop query int false "置顶  1:否  2:是"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /shop/notice/page [get]
func (n *NoticeController) Page(ctx *gin.Context) {
	form := request.NoticePageRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	resolver := pagehelper.New().
		SetOffset(&form).SetFilter([]string{"id"}).
		IsPush(form.Keyword != "", queryWrapper.Like("content", form.Keyword)).IsPush(form.Status > 0, queryWrapper.Eq("status", form.Status)).
		IsPush(form.IsTop > 0, queryWrapper.Eq("is_top", form.IsTop)).Paginator()

	page, err := business.ServiceImpl().NoticeService.FindPage(ctx, resolver)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}
	serializer.SuccessData(ctx, "", page)
}

// @Tags 公告管理
// @description: 新增公告
// @Summary 新增公告
// @Security ApiKeyAuth
// @function: Save
// @Accept  json
// @Produce  json
// @Param data body request.NoticeRequest true "请求参数"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /shop/notice [post]
func (n *NoticeController) Save(ctx *gin.Context) {
	form := request.NoticeRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	if err := business.ServiceImpl().NoticeService.Crate(ctx, &model.Notice{
		ShopId:  constant.SHOP_ID,
		Title:   form.Title,
		Content: form.Content,
		Status:  form.Status,
		IsTop:   form.IsTop,
	}, form.FileIds); err != nil {
		serializer.Fail(ctx, serializer.PublicCreateErr)
		return
	}

	serializer.Success(ctx, "")
}

// @Tags 公告管理
// @description: 修改公告
// @Summary 修改公告
// @Security ApiKeyAuth
// @function: Update
// @Accept  json
// @Produce  json
// @Param data body request.NoticeRequest true "请求参数"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /shop/notice [put]
func (n *NoticeController) Update(ctx *gin.Context) {
	form := request.NoticeRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	if form.Id < 1 {
		serializer.Fail(ctx, "Id不能为空")
		return
	}

	if err := business.ServiceImpl().NoticeService.Updates(ctx, &form); err != nil {
		serializer.Fail(ctx, serializer.PublicUpdateErr)
		return
	}

	serializer.Success(ctx, "")
}

// @Tags 公告管理
// @description: 通过id查询公告管理
// @Summary 通过id查询公告管理
// @Security ApiKeyAuth
// @function: Info
// @Accept  json
// @Produce  json
// @Param id path int true "id"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /shop/notice/info/{id} [get]
func (n *NoticeController) Info(ctx *gin.Context) {
	id, err := contextArgs.GetUriIntPath(ctx, "id")
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	notices, err := business.ServiceImpl().NoticeService.FindById(ctx, uint(id))
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}
	serializer.SuccessData(ctx, "", notices)
}

// @Tags 公告管理
// @description: 删除公告
// @Summary 删除公告
// @Security ApiKeyAuth
// @function: Delete
// @Accept  json
// @Produce  json
// @Param data body request.CommonBatchRequest true "请求参数"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /shop/notice [delete]
func (n *NoticeController) Delete(ctx *gin.Context) {
	form := request.CommonBatchRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	if err := business.ServiceImpl().NoticeService.DeleteBatch(ctx, form.Ids); err != nil {
		serializer.Fail(ctx, serializer.PublicDeleteErr)
		return
	}

	serializer.Success(ctx, "")
}
